#include "../../h/apue.h"
#include <fcntl.h>

int main(void)
{
//    int fd;
    char * filename = "test_trunc.txt";

    //int bufsize = 4096;
//    char * data = "abcdefghijklmnopqrstuvwxyz\n";
//    int i;
//
//    if ((fd = open("test_trunc.txt", O_WRONLY | O_CREAT | O_TRUNC, FILE_MODE)) == -1)
//        err_sys("create file test_trunc.txt error.");
//
//    for (i=0; i<10000; ++i)
//        if (write(fd, data, strlen(data)) < (int)strlen(data))
//            err_sys("write data error for test.trunc.txt");

    off_t size;
    struct stat st;
    if (stat(filename, &st) == -1)
        err_sys("stat file %s error", filename);

    size =st.st_size;

    /* test truncate */
    if (truncate(filename, size-2) == -1)
        err_sys("truncate file %s error", filename);
    else
        printf("truncate file %s 2 byte.\n", filename);

    return 0;
}
